package com.googlecode.bip.dao;

import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.TimeZone;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import com.googlecode.bip.entity.IQuestion;
import com.googlecode.bip.entity.ITopic;
import com.googlecode.bip.entity.IUser;
import com.googlecode.bip.entity.Question;
import com.googlecode.bip.entity.Topic;
import com.googlecode.bip.entity.User;
import cx.ath.choisnet.sql.DataSourceFactory;

public class BipDAOTestHelper
{
    private static boolean initDone = false;

    // TODO: improve this
    private static final String dbdriverClassName = "com.mysql.jdbc.Driver";
    // TODO: improve this
    private static final String dburl = "jdbc:mysql://localhost:3306/BiPol?autoReconnect=true";
    // TODO: improve this
    private static final String dbusername = "bipol";
    // TODO: improve this
    private static final String dbpassword = "bipolpass";

    private static final PrintWriter dblogger = new PrintWriter( System.err );

    public synchronized static void setUpClass() throws Exception
    {
        if( initDone ) {
            return;
            }
        // System.out.println( System.getProperties() );

        // setup the jndi context and the datasource
        try {
            // Create initial context
            System.setProperty(
                Context.INITIAL_CONTEXT_FACTORY,
                "org.apache.naming.java.javaURLContextFactory"
                );
            System.setProperty(
                Context.URL_PKG_PREFIXES,
                "org.apache.naming"
                );

            InitialContext ic = new InitialContext();

            ic.createSubcontext("java:");
            ic.createSubcontext("java:comp");
            ic.createSubcontext("java:comp/env");
            ic.createSubcontext("java:comp/env/jdbc");

            // Construct DataSource
            //OracleConnectionPoolDataSource ds = new OracleConnectionPoolDataSource();
            DataSource ds = DataSourceFactory.buildDataSource(
                    dbdriverClassName,
                    dburl,
                    dbusername,
                    dbpassword,
                    dblogger
                    );

            ic.bind("java:comp/env/jdbc/bipol", ds);
            }
        catch( NamingException e ) {
            Logger.getLogger( BipDAOTestHelper.class ).fatal( e );
            throw e;
            }

        initDone = true;
        Logger.getLogger( BipDAOTestHelper.class ).info( "setUpClass done" );
   }

    public static ITopic createTopic()
        //throws NamingException, SQLException
    {
        return createTopic( "fr" );
    }

    public static ITopic createTopic( String idlocal )
        //throws NamingException, SQLException
    {
        TopicDAO    dao     = new TopicDAO();
        ITopic      topic   = new Topic();

        topic.setParentIdTopic( 0 );
        topic.setIdLocal( idlocal );
        topic.setName( "^TEST^ Topic - name " + idlocal );
        topic.setDescription( "^TEST^ Topic " + idlocal  );

        try {
            boolean r = dao.insertTopic( topic );

            if( r ) {
                return topic;
                }
            }
        catch( NamingException e ) {
            e.printStackTrace(); // Well, just for running test
            }
        catch( SQLException e ) {
            e.printStackTrace(); // Well, just for running test
            }

        return null;
    }

    public static void deleteTopic( ITopic topic )
    {
        TopicDAO dao = new TopicDAO();

        try {
            dao.deleteTopic( topic.getIdTopic() );
            }
        catch( NamingException e ) {
            e.printStackTrace(); // Well, just for running test
            }
        catch( SQLException e ) {
            e.printStackTrace(); // Well, just for running test
            }
    }

    public static IQuestion createQuestion( ITopic topic )
    {
        return createQuestion( topic, "fr" );
    }

    public static IQuestion createQuestion( ITopic topic, String idlocal )
    {
        QuestionDAO    dao  = new QuestionDAO();
        IQuestion      q    = new Question();

        q.setParentIdTopic( topic.getIdTopic() );
        q.setIsActive( 1 );
        q.setMinValue( -3 );
        q.setMaxValue( 3 );
        q.setDefaultValue( 0 );

        q.setIdLocal( idlocal );
        q.setName( "^TEST^ Question - name " + idlocal );
        q.setDescription( "^TEST^ Question - description " + idlocal  );

        try {
            boolean r = dao.insertQuestion( q );

            if( r ) {
                return q;
                }
            }
        catch( NamingException e ) {
            e.printStackTrace(); // Well, just for running test
            }
        catch( SQLException e ) {
            e.printStackTrace(); // Well, just for running test
            }

        return null;
    }

    public static void deleteQuestion( IQuestion q )
    {
        QuestionDAO dao = new QuestionDAO();

        try {
            dao.deleteQuestion( q.getIdQuestion() );
            }
        catch( NamingException e ) {
            e.printStackTrace(); // Well, just for running test
            }
        catch( SQLException e ) {
            e.printStackTrace(); // Well, just for running test
            }
    }

    public static IUser createUser()
    {
        final String uName = "TESTusername";
        final String uPass = "^TEST^password^";

        IUser user = new User();

        user.setUsername( uName );
        user.setPassword( uPass );
        user.setIdlocal( "fr" );
        user.setTimeZone( TimeZone.getDefault() );
        user.setIsActive( 1 );

        UserDAO dao = new UserDAO();
        boolean r = dao.insertUser( user );

        if( r ) {
            return user;
            }
        System.err.println( "can not create user (already exists?)" );
        return null;
    }

    public static void deleteUser( final IUser user )
    {
        UserDAO dao = new UserDAO();

        try {
            dao .deleteUser( user );
        }
        catch( NamingException e ) {
            e.printStackTrace(); // Well, just for running test
            }
        catch( SQLException e ) {
            e.printStackTrace(); // Well, just for running test
            }
    }

}
